/* Copyright (C) 2010 Luca Piccinelli
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * File:   NormalBayesModel.h
 * Author: picci
 *
 * Created on December 4, 2010, 11:22 AM
 */

#ifndef NORMALBAYESMODEL_H
#define	NORMALBAYESMODEL_H

#include "cv.h"

#include "gestface.h"

namespace NAMESPACE{

// ****** Interface definition *************************************************
class NormalBayesModel{
public:
    virtual ~NormalBayesModel(){}

    virtual NormalBayesModel& train(const cv::Mat& train_data, const cv::Mat& responses, bool update = false) = 0;
    virtual void predict(const cv::Mat& samples, cv::Mat& responses) const = 0;

    virtual bool is_trained() const = 0;
    virtual NormalBayesModel& swap(NormalBayesModel& m) = 0;
};
// -----------------------------------------------------------------------------

// ****** Abstract class definition ********************************************
class AbstractNormalBayesModel : public NormalBayesModel{

public:
    virtual ~AbstractNormalBayesModel();
    AbstractNormalBayesModel();
    AbstractNormalBayesModel(const AbstractNormalBayesModel& nbm);
    AbstractNormalBayesModel& operator= (const AbstractNormalBayesModel& nbm);

    virtual NormalBayesModel& train(const cv::Mat& train_data, const cv::Mat& responses, bool update = false) = 0;
    virtual void predict(const cv::Mat& samples, cv::Mat& responses) const = 0;

    virtual bool is_trained() const = 0;
    virtual NormalBayesModel& swap(NormalBayesModel& m) = 0;
};
// -----------------------------------------------------------------------------

// ****** Discret2D model definition *********************************************
class Discret2DNormalBayesModel : public AbstractNormalBayesModel{
private:
    std::vector<cv::Mat>* with_evidence_data_lut; ///< Look up tables of the probability of each feature vector, given the evidence of the owning class
    cv::Mat* a_priori_data_lut;                  ///< Look up table of the a priori probability of each feature vector
    cv::Mat* a_priori_evidence;                  ///< A priori probability of each class

    cv::Point2i range_begin;
    cv::Point2i range_end;

    int tot_train_counter;
    std::vector<int> class_train_counters;
    std::vector<cv::Mat>* with_evidence_data_lut_counters;
    cv::Mat* a_priori_data_lut_counters;

public:
    virtual ~Discret2DNormalBayesModel();
    Discret2DNormalBayesModel();
    Discret2DNormalBayesModel(const Discret2DNormalBayesModel& nbm);
    Discret2DNormalBayesModel& operator= (const Discret2DNormalBayesModel& nbm);

    Discret2DNormalBayesModel(const cv::Point2i& _range_begin, const cv::Point2i& _range_end);
    Discret2DNormalBayesModel(int bx, int by, int ex, int ey);

    NormalBayesModel& train(const cv::Mat& train_data, const cv::Mat& responses, bool update = false);
    void predict(const cv::Mat& samples, cv::Mat& responses) const;

    std::vector<cv::Mat>& get_with_evidence_data_luts() const ;
    cv::Mat& get_a_priori_data_lut() const ;
    cv::Mat& get_a_priori_evidence() const ;

    Discret2DNormalBayesModel& set_with_evidence_data_luts(std::vector<cv::Mat>* luts);

    bool is_trained() const;
    NormalBayesModel& swap(NormalBayesModel& m);
};
// -----------------------------------------------------------------------------

// ****** Continous model definition *******************************************
class ContinuousNormalBayesModel : public AbstractNormalBayesModel{
public:
    virtual ~ContinuousNormalBayesModel();
    ContinuousNormalBayesModel();
    ContinuousNormalBayesModel(const ContinuousNormalBayesModel& nbm);
    ContinuousNormalBayesModel& operator= (const ContinuousNormalBayesModel& nbm);

    NormalBayesModel& train(const cv::Mat& train_data, const cv::Mat& responses, bool update = false);
    void predict(const cv::Mat& samples, cv::Mat& responses) const;

    bool is_trained() const;
    NormalBayesModel& swap(NormalBayesModel& m) = 0;
};
// -----------------------------------------------------------------------------

}
#endif	/* NORMALBAYESMODEL_H */
